package com.wxc.wxcwzq.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.wxc.wxcwzq.core.util.ReturnUtil;
import com.wxc.wxcwzq.web.service.SysUserService;

@Controller
@RequestMapping("/wxc")
public class BaseController {

	//管理员账号：admin/123456

	@Autowired
	private SysUserService userService;
	/**
	 * 登录
	 * @return
	 */
	@GetMapping("/login")
	public String loginPage(){
		return "page/login/login";
	}
	
	@PostMapping("/login")
	@ResponseBody
	public String userLoging(String userName,String userPwd){
		System.out.println("我要开始登录了！"+userName+"--"+userPwd);
		Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName,userPwd);
        ReturnUtil result = new ReturnUtil();
        try{
        	subject.login(token);
            Session session=subject.getSession();
            session.setAttribute("user",token.getPrincipal());
            result.setResult(true);
            System.out.println("测试成功！");
        }catch(UnknownAccountException une){
        	result.setResult(false);
        	result.setMsg("账号不存在！");
        	System.out.println("账号不存在！");
        }catch (IncorrectCredentialsException ice) {
        	result.setResult(false);
        	result.setMsg("邮箱/密码不匹配！");
            System.out.println("邮箱/密码不匹配！");
        } catch (LockedAccountException lae) {
        	result.setResult(false);
        	result.setMsg("账户已被冻结！");
            System.out.println("账户已被冻结！");
        } catch (AuthenticationException ae) {
        	result.setResult(false);
        	result.setMsg("用户名密码错误！");
            System.out.println("用户名密码错误！" + ae.getMessage());
        }
//		MD5Util.encodeMd5Salt(loginName, password, salt)
		return JSON.toJSONString(result);
	}
	
	@RequestMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "page/403";
    }
	
	/**
	 * 注销登录
	 * @return
	 */
	@RequestMapping("/logout")
	public String loginOut(){
		return "page/login/login";
	}
	@RequestMapping("/index")
//	@RequiresPermissions("")
	public String loadIndex(){
		return "index";
	}	
}
